<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            background-color: #f8f9fa;
            background-image: url('/image/backgd.jpg');
            background-size: cover;
            background-position: center;
            background-repeat: no-repeat;
            margin: 0;
            height: 100vh;
            overflow-y: hidden;
        }
        .login-container {
            margin-top: 100px;
        }
        .login-form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body>
<div class="container login-container">
    <div class="row justify-content-center">
        <div class="col-md-6">
            <div class="login-form">
                <h2 class="text-center mb-4">登录</h2>
                <form id="loginForm">
                    <div class="form-group">
                        <label for="username">用户名</label>
                        <input type="text" class="form-control" id="username" name="username" required>
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" name="password" required>
                    </div>
                    <label>选择登录身份</label>
                    <div class="form-group" style="justify-content: center; display: flex;align-content: center">

                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="userType" id="studentRadio" value="student" checked>
                            <label class="form-check-label" for="studentRadio">
                                学生
                            </label>
                        </div>
                        <div class="form-check">
                            <input class="form-check-input" type="radio" name="userType" id="dormManagerRadio" value="dormManager">
                            <label class="form-check-label" for="dormManagerRadio">
                                宿管
                            </label>
                        </div>
                    </div>
                    <button type="submit" class="btn btn-primary btn-block">登录</button>
                </form>
                <p class="text-center mt-3">
                    <a href="/register">还没有账号？立即注册</a>
                </p>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function () {
        $('#loginForm').on('submit', function (event) {
            event.preventDefault(); // 阻止表单默认提交行为

            var formData = {
                username: $('#username').val(),
                password: $('#password').val()
            };
            var userType = $('input[name="userType"]:checked').val();
            var loginUrl = userType === 'student' ? '/student/login' : '/dorManager/login';
            $.ajax({
                url: loginUrl,
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(formData),
                success: function (response) {
                    // 处理成功响应
                    console.log(response);
                    if (response.code === 200) {
                        alert('登录成功');
                        localStorage.setItem('userInfo', JSON.stringify(response.data))
                        // 重定向到其他页面
                        window.location.href = '/home';
                    } else {
                        alert('登录失败');
                    }
                },
                error: function (xhr, status, error) {
                    // 处理错误响应
                    console.error(error);
                    alert('登录失败');
                }
            });
        });
    });
</script>
</body>
</html>
    